﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/


#ifndef Rect_h__
#define Rect_h__

#include <d2d1.h>
#include "../utility/MacroTool.h"
#include "Vector.h"
#include "Size.h"

namespace SkyEngine2d
{
	class Line;

	//! 矩形
	class SGE_API Rect
	{
	public:
		//! 构造
		Rect();

		//! 构造
		/*!
		\param float left_bottom_x 左下角的坐标 x
		\param float left_bottom_y 左下角的坐标 y
		\param float width 尺寸 宽度
		\param float height 尺寸 高度
		*/
		Rect(float left_bottom_x, float left_bottom_y, float width, float height);

		//! 构造
		/*!
		\param float left_bottom_x 左下角的坐标 x
		\param float left_bottom_y 左下角的坐标 y
		\param const Size & size 尺寸
		*/
		Rect(float left_bottom_x, float left_bottom_y, const Size& size);

		//! 构造
		/*!
		\param const Vector2 & left_bottom_pos  左下角的坐标
		\param const Size & size 尺寸
		*/
		Rect(const Vector2& left_bottom_pos, const Size& size);

		//! 拷贝构造
		/*!
		\param const Rect & rect
		*/
		Rect(const Rect& rect);

		//! 移动一段距离
		/*!
		\param const Vector2 & dt 移动的增量
		\return void
		*/
		void move(const Vector2& dt);

		//! 移动一段距离
		/*!
		\param float x_dt 移动的增量
		\param float y_dt 移动的增量
		\return void
		*/
		void move(float x_dt, float y_dt);

		//! 移动到某个位置
		/*!
		\param const Vector2 & pos目标点
		\return void
		*/
		void moveTo(const Vector2& pos);

		//! 移动到某个位置
		/*!
		\param float pos_x 目标点 x
		\param float pos_y 目标点 y
		\return void
		*/
		void moveTo(float pos_x, float pos_y);

		//! 点是否被矩形包含
		/*!
		\param const Vector2 & point 点
		\return bool
		*/
		bool containPoint(const Vector2& point)const;

		//! 线段是否与矩形相交
		/*!
		\param const Line & line 线段
		\return bool
		*/
		bool intersectWithLine(const Line& line)const;

		//! 两个矩形是否相交
		/*!
		\param const Rect & rect 矩形
		\return bool
		*/
		bool intersectsWithRect(const Rect& rect)const;

		//! 重载相等运算符 
		/*!
		\param const Rect & r
		\return bool
		*/
		bool operator == (const Rect& r)const;

		//! 与D2d 类型转换
		/*!
		\return
		*/
		operator D2D1_RECT_F();

		//! 矩形左边的x坐标
		/*!
		\return float
		*/
		float getLeft()const { return m_left; }

		//! 矩形右边的x坐标
		/*!
		\return float
		*/
		float getRight()const { return m_left + m_size.width; }

		//! 矩形顶部的y坐标
		/*!
		\return float
		*/
		float getTop()const { return m_bottom + m_size.height; }

		//! 矩形底部的y坐标
		/*!
		\return float
		*/
		float getBottom()const { return m_bottom; }

		//! 矩形左下角的坐标
		/*!
		\return Vector2
		*/
		Vector2 getBottomLeft()const { return Vector2(m_left, m_bottom); }

		//！矩形的左上角的坐标 
		/*!
		\return ForwardDeclare::SkyEngine2d::Vector2
		*/
		Vector2 getTopLeft()const { return Vector2(m_left, getTop()); }
		
		//! 矩形右下角的坐标
		/*!
		\return ForwardDeclare::SkyEngine2d::Vector2
		*/
		Vector2 getBottomRight()const { return Vector2(getRight(), m_bottom); }

		//! 矩形右上角的坐标
		/*!
		\return ForwardDeclare::SkyEngine2d::Vector2
		*/
		Vector2 getTopRight()const { return Vector2(getRight(), getTop()); }

		//! 设置矩形的左边的x坐标
		/*!
		\param float left
		\return void
		*/
		void setLeft(float left) { m_left = left; }

		//! 设置矩形的右边的x坐标
		/*!
		\param float right
		\return void
		*/
		void setRight(float right) { m_size.width = right - m_left; }

		//! 设置矩形的上边的y坐标
		/*!
		\param float top
		\return void
		*/
		void setTop(float top) { m_size.height = top - m_bottom; }

		//! 设置矩形的下边的y坐标
		/*!
		\param float bottom
		\return void
		*/
		void setBottom(float bottom) { m_bottom = bottom; }

		//! 获取矩形的尺寸
		/*!
		\return const SkyEngine2d::Size&
		*/
		const Size& getSize()const { return m_size; }

		//! 获取矩形的宽
		/*!
		\return float
		*/
		float getWidth()const { return m_size.width; }

		//! 获取矩形的高
		/*!
		\return float
		*/
		float getHeight()const { return m_size.height; }

		//! 设置矩形的尺寸
		/*!
		\param const Size & size
		\return void
		*/
		void setSize(const Size& size) { m_size = size; }

		//! 设置矩形的宽
		/*!
		\param float w
		\return void
		*/
		void setWidth(float w) { m_size.width = w; }

		//! 设置矩形的高
		/*!
		\param float h
		\return void
		*/
		void setHeight(float h) { m_size.height = h; }

		//! 设置左下角的坐标
		/*!
		\param const Vector2 & bottom_left
		\return void
		*/
		void setBottomLeft(const Vector2& bottom_left);

	private:
		float m_left;
		float m_bottom;
		Size m_size;
	public:
		static Rect Zero;
	};
}


#endif // Rect_h__
